<?php
namespace app\index\controller;
// use app\index\
class Article extends Base
{
    public function index()
    {
        $id=request()->param('id'); 
        $viewPage=$this->getIndexPage($id);
        // 分页
        $p=request()->param('p'); 
        if(!$p){
            $p=1;
        }
        $psize=20;        
        $stp=($p-1)*$psize;

        $article=db("cms_article");
        $channel=db("channel");

        $pid=$channel->where("id=".$id)->value("pid");
        $where['status']=1;
        if($pid > 0){
            $gcid=$pid;
            $pcid=$id;
            $lchannel=$this->get_left_channel($pid);
            $fname=$channel->where("id",$pid)->value("name");

            $where['cid']=$pcid;    
            $count=$article->where($where)->count();
            $tolp=ceil($count/$psize);
            $mess=$article->where($where)->order("sort asc , id DESC")->limit($stp,$psize)->select();
            $pnavName=$channel->where("id=".$pcid)->value("name");
            // $viewPage="list";  
        }else{
            $gcid=$id;
            // $pcid=$channel->where("pid=".$cid)->value("id");
            $lchannel=$this->get_left_channel($id);
            $fname=$channel->where("id",$id)->value("name");
            $cid_arr=db("channel")->where("pid=".$gcid)->column("id");
            // print_r($cid_arr);exit();
            // $where['is_channel']=1;
            $where['cid']=array("in",$cid_arr);
            // $mess['tznews']=$article->where($where)->order("sort asc , id DESC")->limit("7")->select();//通知公告
            $where['status']=1;
            // $where['cid']=$gcid;   
            $count=$article->where($where)->count();
            $tolp=ceil($count/$psize);
            $mess=$article->where($where)->order("sort asc , id DESC")->limit($stp,$psize)->select();
            $pnavName=$channel->where("id=".$gcid)->value("name");
            // $where['is_home']=1;

            // $gdwhere['thumb']=array("neq",'');
            // $mess['gdnews']=db("article")->where($gdwhere)->order("sort asc ,id desc")->limit(5)->select();
            // for ($i=0; $i < count($mess['gdnews']); $i++) { 
            //     if($mess['gdnews'][$i]['thumb']){
            //         $mess['gdnews'][$i]['annexes']=db("annex")->where("id=".$mess['gdnews'][$i]['thumb'])->value("thumb");  
            //     }
            // }
            // print_r($mess['gdnews']);exit;
            // $hotwhere['thumb']=array("eq",'');
            // $mess['hotnews']=db("article")->where($ltwhere)->order("sort asc ,id desc")->limit(7)->select();
            // for ($i=0; $i < count($mess['hotnews']); $i++) { 
            //     $mess['hotnews'][$i]['cname']=db("channel")->where("id=".$mess['hotnews'][$i]['cid'])->value("name");
            // }
            // $listwhere['status']=1;
            // $mess['listnews']=$article->where($listwhere)->order("sort asc,id desc")->limit("50")->select();//公司新闻
            // for ($i=0; $i < count($mess['listnews']); $i++) { 
            //     $mess['listnews'][$i]['cname']=db("channel")->where("id=".$mess['listnews'][$i]['cid'])->value("name");
            // }
            // $viewPage="index";
        }

        // 分页渲染
        $this->assign("p",$p);
        $this->assign("tolp",$tolp);
        $this->assign("count",$count);

        $gnavName=$channel->where("id=".$gcid)->value("name");

        for ($i=0; $i < count($mess); $i++) { 
            if($mess[$i]['thumb']){
                $mess[$i]['annexes']=db("filex")->where("id=".$mess[$i]['thumb'])->value("thumb");
            }
            if($mess[$i]['filex']){
                $mess[$i]['fileshow']=db("filex")->where("id=".$mess[$i]['filex'])->value("thumb");
            }
            if($mess[$i]['images']){
                $annex_ids=explode(",",$mess[$i]['images']);
                $annexWh['id']=array("in",$annex_ids);
                $mess[$i]['photos']=db("filex")->where($annexWh)->select();
            }
            $mess[$i]['createtime']=date("Y-m-d",$mess[$i]['createtime']);
            // $year=explode("-",$mess[$i]['createtime'])[0];
            // $mess[$i]['year']=$year;
            // $data[$i]['year']=$year;
            // $mess[$i]['child'][]=$mess[$i];
        }
        // print_r($mess);exit();
        // SEO META关键字配置
        $meta=$this->meta;
        $meta['title']=$pnavName.' '.$gnavName.' '.$meta['title'];

        $meta['keywords']=$pnavName.$gnavName.$meta['keywords'];

        $meta['descript']=$pnavName.$gnavName.$meta['descript'];

        $this->assign("meta",$meta);
        
        $modelx=$channel->where("id",$gcid)->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);
        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("id",$id);
        $this->assign("pcid",$pcid);
        $this->assign("pnavName",$pnavName);
        $this->assign("gnavName",$gnavName);
        $this->assign("mess",$mess);
        $this->assign("lchannel",$lchannel);
        $this->assign("gcid",$gcid);
        $this->assign("pcid",$pcid);
        $this->assign("fname",$fname);            
        return view($viewPage);
       
    }    
    public function detail()
    {
        $id=request()->param('id');       
        $article=db("cms_article");
        $channel=db("channel");

        $where['id']=$id;
        $mess=$article->where($where)->find();
        if($mess['thumb']){
            $mess['annexes']=db("filex")->where("id=".$mess['thumb'])->value("thumb");
        }
        if($mess['filex']){
            $mess['fileshow']=db("filex")->where("id=".$mess['filex'])->value("thumb");
        }
        // print_r($mess);exit;
        $pcid=$mess['cid'];
        $viewPage=$this->getDetailPage($pcid);
        
        $gcid=$channel->where("id=".$pcid)->value("pid");
        
        // $where['status']=1;
        
        $fname=$channel->where("id",$gcid)->value("name");
        $lchannel=$this->get_left_channel($gcid);
        $gnavName=$channel->where("id=".$gcid)->value("name");
        $pnavName=$channel->where("id=".$pcid)->value("name");

        // SEO META关键字配置
        $meta=$this->meta;
        $meta['title']=$mess['title'].' '.$pnavName.' '.$gnavName.' '.$meta['title'];
        if($mess['keywords']){
            $meta['keywords']=$mess['keywords'];
        }else{
            $meta['keywords']=$mess['title'].$pnavName.$gnavName.$meta['keywords'];
        }
        if($mess['descript']){
            $meta['descript']=$mess['descript'];
        }else{
            $meta['descript']=$mess['title'].$pnavName.$gnavName.$meta['descript'];
        }
        $this->assign("meta",$meta);

        $modelx=$channel->where("id",$gcid)->value("modelx");
        $modelxMark=db("modelx")->where("id=".$modelx)->value("mark");
        $this->assign("modelxMark",$modelxMark);
        $banner=$this->get_banner($gcid);
        $this->assign("banner",$banner);
        $this->assign("pnavName",$pnavName);
        $this->assign("gnavName",$gnavName);
        $this->assign("lchannel",$lchannel);
        $this->assign("pcid",$pcid);
        $this->assign("gcid",$gcid);
        $this->assign("fname",$fname);
        $this->assign("mess",$mess);
        return view($viewPage);
    }
}
